home *** CD-ROM | disk | FTP | other *** search
/ Champak 141 / (Vol 141) Oct 17 2011.iso / Games / Clueless.swf / scripts / DressupEngine.as < prev    next >
Encoding:
Text File  |  2011-10-17  |  13.7 KB  |  439 lines

  1. package
  2. {
  3.    import Common.SoundManager;
  4.    import flash.display.DisplayObject;
  5.    import flash.display.DisplayObjectContainer;
  6.    import flash.display.MovieClip;
  7.    import flash.events.Event;
  8.    import flash.events.EventDispatcher;
  9.    import flash.events.MouseEvent;
  10.    import flash.geom.Point;
  11.    import flash.media.SoundChannel;
  12.    import flash.text.TextField;
  13.    
  14.    public class DressupEngine extends EventDispatcher
  15.    {
  16.       
  17.       public static const DressupEnginePickup:String = "DressupEnginePickup";
  18.       
  19.       public static const DressupEngineDrop:String = "DressupEngineDrop";
  20.        
  21.       
  22.       public var DebugText:TextField = null;
  23.       
  24.       internal var _aslots:Array;
  25.       
  26.       public var AlwaysActive:* = true;
  27.       
  28.       internal var _amodels:Array;
  29.       
  30.       internal var _partCurrent:Part = null;
  31.       
  32.       internal var _modelActive:Model = null;
  33.       
  34.       internal var _bPickup:Boolean = true;
  35.       
  36.       internal var _scVoiceOvers:SoundChannel = null;
  37.       
  38.       internal var _bEnabled:Boolean = true;
  39.       
  40.       internal var _doc:DisplayObjectContainer;
  41.       
  42.       internal var _pCurrentOffset:Point;
  43.       
  44.       public function DressupEngine(param1:DisplayObjectContainer, param2:Array, param3:Array)
  45.       {
  46.          var _loc4_:DisplayObject = null;
  47.          AlwaysActive = true;
  48.          _bEnabled = true;
  49.          _bPickup = true;
  50.          _amodels = new Array();
  51.          _aslots = new Array();
  52.          _partCurrent = null;
  53.          _pCurrentOffset = new Point();
  54.          DebugText = null;
  55.          _modelActive = null;
  56.          _scVoiceOvers = null;
  57.          super();
  58.          _doc = param1;
  59.          _aslots = param2;
  60.          _amodels = param3;
  61.          _doc.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove,false,0,true);
  62.          for each(_loc4_ in _aslots)
  63.          {
  64.             _loc4_.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown,false,0,true);
  65.          }
  66.          for each(_loc4_ in _amodels)
  67.          {
  68.             _loc4_.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown,false,0,true);
  69.          }
  70.          ActiveModel = _amodels[0];
  71.       }
  72.       
  73.       internal function onPickupClothes() : void
  74.       {
  75.          SoundManager.getInstance().playSound("PickupItemSound");
  76.          dispatchEvent(new Event(DressupEnginePickup));
  77.       }
  78.       
  79.       public function get Enabled() : Boolean
  80.       {
  81.          return _bEnabled;
  82.       }
  83.       
  84.       public function get ActiveModel() : Model
  85.       {
  86.          return _modelActive;
  87.       }
  88.       
  89.       protected function returnCurrent() : void
  90.       {
  91.          if(_partCurrent != null)
  92.          {
  93.             _doc.removeChild(_partCurrent);
  94.             reslotPart(_partCurrent);
  95.             _partCurrent = null;
  96.          }
  97.       }
  98.       
  99.       protected function setCurrent(param1:Part) : void
  100.       {
  101.          _partCurrent = param1;
  102.          _pCurrentOffset = new Point(-_partCurrent.width / 2,-_partCurrent.height / 2);
  103.          _partCurrent.Pose = ModelPose.DEFAULT;
  104.          _partCurrent.x = _doc.mouseX + _pCurrentOffset.x;
  105.          _partCurrent.y = _doc.mouseY + _pCurrentOffset.y;
  106.          _doc.addChild(_partCurrent);
  107.       }
  108.       
  109.       protected function playPutClothingSoundFor(param1:Model) : void
  110.       {
  111.          if(param1.name.indexOf("model1") >= 0)
  112.          {
  113.             SoundManager.getInstance().playSound("PutClothingSound1");
  114.             FeedbackClothes((param1 as DressupModel).ModelTheme);
  115.          }
  116.          else if(param1.name.indexOf("model0") >= 0)
  117.          {
  118.             SoundManager.getInstance().playSound("PutClothingSound2");
  119.             FeedbackClothes((param1 as DressupModel).ModelTheme);
  120.          }
  121.          else if(param1.name.indexOf("model2") >= 0)
  122.          {
  123.             SoundManager.getInstance().playSound("PutClothingSound3");
  124.             FeedbackClothes((param1 as DressupModel).ModelTheme);
  125.          }
  126.          else
  127.          {
  128.             SoundManager.getInstance().playSound("PutClothingSound1");
  129.          }
  130.       }
  131.       
  132.       public function set Pickup(param1:Boolean) : void
  133.       {
  134.          _bPickup = param1;
  135.       }
  136.       
  137.       public function set Enabled(param1:Boolean) : void
  138.       {
  139.          _bEnabled = param1;
  140.       }
  141.       
  142.       public function stopVoiceOver() : void
  143.       {
  144.          if(_scVoiceOvers != null)
  145.          {
  146.             _scVoiceOvers.stop();
  147.          }
  148.       }
  149.       
  150.       public function set ActiveModel(param1:Model) : *
  151.       {
  152.          deactivateAllModels();
  153.          _modelActive = param1;
  154.          _modelActive.Active = true;
  155.       }
  156.       
  157.       protected function onMouseDown(param1:MouseEvent) : void
  158.       {
  159.          var _loc2_:Boolean = false;
  160.          var _loc3_:Boolean = false;
  161.          var _loc4_:ClothingSlot = null;
  162.          var _loc5_:Part = null;
  163.          var _loc6_:int = 0;
  164.          var _loc7_:Model = null;
  165.          var _loc8_:Part = null;
  166.          var _loc9_:Part = null;
  167.          _loc2_ = false;
  168.          _loc3_ = false;
  169.          _loc6_ = 0;
  170.          while(_loc6_ < _aslots.length)
  171.          {
  172.             if((_loc4_ = _aslots[_loc6_] as ClothingSlot).getBounds(_doc).containsPoint(new Point(_doc.mouseX,_doc.mouseY)))
  173.             {
  174.                _loc2_ = true;
  175.                _loc8_ = null;
  176.                if(_loc4_.hasPart() && _partCurrent == null)
  177.                {
  178.                   if(_loc4_.getPart().hasPixel(_doc.mouseX,_doc.mouseY,_doc))
  179.                   {
  180.                      if(_bPickup)
  181.                      {
  182.                         _loc8_ = _loc4_.pickupPart();
  183.                      }
  184.                      else if((_loc8_ = _loc4_.getPart()) != null)
  185.                      {
  186.                         _loc8_ = _loc8_.Template.createPart();
  187.                      }
  188.                   }
  189.                   else if(_loc4_.getPart().Category == "body")
  190.                   {
  191.                      if((_loc8_ = _loc4_.getPart()) != null)
  192.                      {
  193.                         _loc8_ = _loc8_.Template.createPart();
  194.                      }
  195.                   }
  196.                }
  197.                if(_loc8_ != null)
  198.                {
  199.                   _loc8_.x = _doc.mouseX + _pCurrentOffset.x;
  200.                   _loc8_.y = _doc.mouseY + _pCurrentOffset.y;
  201.                   if(_partCurrent != null)
  202.                   {
  203.                      returnCurrent();
  204.                      setCurrent(_loc8_);
  205.                      _loc3_ = true;
  206.                   }
  207.                   else if(_loc8_ != _partCurrent)
  208.                   {
  209.                      returnCurrent();
  210.                      setCurrent(_loc8_);
  211.                      _loc3_ = true;
  212.                   }
  213.                   else
  214.                   {
  215.                      returnCurrent();
  216.                      _loc3_ = true;
  217.                   }
  218.                   break;
  219.                }
  220.                if(_partCurrent != null)
  221.                {
  222.                   break;
  223.                }
  224.             }
  225.             _loc6_++;
  226.          }
  227.          if(_loc2_ && !_loc3_ && _partCurrent != null)
  228.          {
  229.             onDropClothes();
  230.             returnCurrent();
  231.          }
  232.          if(_loc3_)
  233.          {
  234.             onPickupClothes();
  235.          }
  236.          for each(_loc7_ in _amodels)
  237.          {
  238.             if(Boolean(_loc7_.getBounds(_doc).containsPoint(new Point(_doc.mouseX,_doc.mouseY))) && _loc7_.visible)
  239.             {
  240.                if(_loc7_.Active || AlwaysActive)
  241.                {
  242.                   ActiveModel = _loc7_;
  243.                   if(_partCurrent != null)
  244.                   {
  245.                      _loc5_ = _loc7_.removePart(_partCurrent.Category);
  246.                      reslotPart(_loc5_);
  247.                      if(_partCurrent.Category == "dress")
  248.                      {
  249.                         _loc5_ = _loc7_.removePart("top");
  250.                         reslotPart(_loc5_);
  251.                         _loc5_ = _loc7_.removePart("bottom");
  252.                         reslotPart(_loc5_);
  253.                      }
  254.                      else if(_partCurrent.Category == "bottom")
  255.                      {
  256.                         _loc5_ = _loc7_.removePart("dress");
  257.                         reslotPart(_loc5_);
  258.                      }
  259.                      else if(_partCurrent.Category == "top")
  260.                      {
  261.                         _loc5_ = _loc7_.removePart("dress");
  262.                         reslotPart(_loc5_);
  263.                      }
  264.                      _loc7_.addPart(_partCurrent);
  265.                      playPutClothingSoundFor(_loc7_);
  266.                      onDropClothes();
  267.                      _partCurrent = null;
  268.                   }
  269.                   else
  270.                   {
  271.                      _partCurrent = _loc7_.pickupPart(new Point(_doc.mouseX,_doc.mouseY),_doc);
  272.                      if(_partCurrent != null)
  273.                      {
  274.                         onPickupClothes();
  275.                         setCurrent(_partCurrent);
  276.                      }
  277.                   }
  278.                }
  279.                else
  280.                {
  281.                   ActiveModel = _loc7_;
  282.                }
  283.             }
  284.          }
  285.          if(_partCurrent != null && DebugText != null)
  286.          {
  287.             DebugText.text = _partCurrent.Template.Name;
  288.          }
  289.       }
  290.       
  291.       public function reslotPart(param1:Part) : ClothingSlot
  292.       {
  293.          var _loc2_:ClothingSlot = null;
  294.          var _loc3_:String = null;
  295.          _loc2_ = null;
  296.          if(param1 != null)
  297.          {
  298.             _loc3_ = param1.Category;
  299.             if(_loc3_ == "dress")
  300.             {
  301.                _loc3_ = "top";
  302.             }
  303.             for each(_loc2_ in _aslots)
  304.             {
  305.                if(!_loc2_.hasPart() && _loc2_.name.indexOf(_loc3_) != -1)
  306.                {
  307.                   param1.Pose = ModelPose.DEFAULT;
  308.                   _loc2_.addChild(param1);
  309.                   param1.center();
  310.                   break;
  311.                }
  312.             }
  313.          }
  314.          return _loc2_;
  315.       }
  316.       
  317.       protected function getPartFromSlot(param1:MovieClip) : Part
  318.       {
  319.          var _loc2_:int = 0;
  320.          var _loc3_:Part = null;
  321.          _loc2_ = 0;
  322.          while(_loc2_ < param1.numChildren)
  323.          {
  324.             if(param1.getChildAt(_loc2_) is Part)
  325.             {
  326.                return param1.getChildAt(_loc2_) as Part;
  327.             }
  328.             _loc2_++;
  329.          }
  330.          return null;
  331.       }
  332.       
  333.       internal function FeedbackClothes(param1:DressupTheme) : void
  334.       {
  335.          var _loc2_:Part = null;
  336.          var _loc3_:int = 0;
  337.          var _loc4_:String = null;
  338.          var _loc5_:String = null;
  339.          _loc2_ = _partCurrent;
  340.          _loc3_ = 0;
  341.          if(_loc2_ != null && param1 != null)
  342.          {
  343.             for each(_loc4_ in param1.TargetAttributes)
  344.             {
  345.                if(param1.Title.toLowerCase().indexOf(_loc4_.toLowerCase()) < 0)
  346.                {
  347.                   if(_loc2_.Template.hasAttribute(_loc4_))
  348.                   {
  349.                      _loc3_++;
  350.                   }
  351.                }
  352.             }
  353.             if(MainDocument.VOICE_OVERS_ENABLED)
  354.             {
  355.                if(_loc3_ > 4)
  356.                {
  357.                   _loc5_ = "positive_" + int(Math.random() * 20 + 1);
  358.                   if(_scVoiceOvers != null)
  359.                   {
  360.                      _scVoiceOvers.stop();
  361.                   }
  362.                   _scVoiceOvers = SoundManager.getInstance().playSound(_loc5_);
  363.                }
  364.                if(_loc3_ == 0)
  365.                {
  366.                   _loc5_ = "negative_" + int(Math.random() * 14 + 1);
  367.                   if(_scVoiceOvers != null)
  368.                   {
  369.                      _scVoiceOvers.stop();
  370.                   }
  371.                   _scVoiceOvers = SoundManager.getInstance().playSound(_loc5_);
  372.                }
  373.             }
  374.          }
  375.       }
  376.       
  377.       public function deactivateAllModels() : void
  378.       {
  379.          var _loc1_:Model = null;
  380.          for each(_loc1_ in _amodels)
  381.          {
  382.             _loc1_.Active = false;
  383.          }
  384.       }
  385.       
  386.       public function removeDuplicates() : void
  387.       {
  388.          var _loc1_:Boolean = false;
  389.          var _loc2_:int = 0;
  390.          var _loc3_:int = 0;
  391.          var _loc4_:ClothingSlot = null;
  392.          var _loc5_:ClothingSlot = null;
  393.          var _loc6_:String = null;
  394.          _loc1_ = true;
  395.          while(_loc1_)
  396.          {
  397.             _loc1_ = false;
  398.             _loc2_ = 1;
  399.             while(_loc2_ < this._aslots.length)
  400.             {
  401.                _loc4_ = _aslots[_loc2_] as ClothingSlot;
  402.                _loc3_ = 0;
  403.                while(_loc3_ < _loc2_)
  404.                {
  405.                   _loc5_ = _aslots[_loc3_] as ClothingSlot;
  406.                   if(_loc4_.hasPart() && _loc5_.hasPart())
  407.                   {
  408.                      if(_loc4_.getPart().Template == _loc5_.getPart().Template)
  409.                      {
  410.                         _loc6_ = _loc5_.getPart().Template.Category;
  411.                         _loc5_.clean();
  412.                         _loc5_.addChild(MainDocument.Package.getRandomTemplate(_loc6_).createPart());
  413.                         _loc1_ = true;
  414.                      }
  415.                   }
  416.                   _loc3_++;
  417.                }
  418.                _loc2_++;
  419.             }
  420.          }
  421.       }
  422.       
  423.       protected function onMouseMove(param1:MouseEvent) : void
  424.       {
  425.          if(_partCurrent != null)
  426.          {
  427.             _partCurrent.x = _doc.mouseX + _pCurrentOffset.x;
  428.             _partCurrent.y = _doc.mouseY + _pCurrentOffset.y;
  429.          }
  430.       }
  431.       
  432.       internal function onDropClothes() : void
  433.       {
  434.          SoundManager.getInstance().playSound("PickupItemSound");
  435.          dispatchEvent(new Event(DressupEngineDrop));
  436.       }
  437.    }
  438. }
  439.